/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package note.dao;

import note.dbc.DBConnection;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import note.vo.User;

public class UserDAO {

    public boolean insert(User user) {
        String sql = "insert into user_table values(?,?,?,?)";
        Connection conn = DBConnection.getConnection();
        PreparedStatement pstmt = null;
        boolean insertSuccessFlag = false;
        try {
            pstmt = conn.prepareStatement(sql);
            pstmt.setInt(1, user.getId());
            pstmt.setString(2, user.getName());
            pstmt.setString(3, user.getPass());
            pstmt.setString(4, user.getEmail());
            //执行pstmt表示的插入命令
            pstmt.executeUpdate();
           
            insertSuccessFlag = true;
        } catch (SQLException e) {
            System.out.println("插入操作中出现错误！！！");
            System.out.println(e);
        } finally {
             DBConnection.close(conn, pstmt, null);
        }
        return insertSuccessFlag;
    }

    // 修改操作
    public boolean update(User user) {
        String sql = "UPDATE user_table SET user_name=?,user_pass=?,email=? WHERE user_id=?";
        Connection conn = DBConnection.getConnection();
        PreparedStatement pstmt = null;
        boolean updateSuccessFlag = false;
        try {
            pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, user.getName());
            pstmt.setString(2, user.getPass());
            pstmt.setString(3, user.getEmail());
            pstmt.setInt(4, user.getId());
            int x = pstmt.executeUpdate();
            if (x > 0) {
                updateSuccessFlag = true;
            }
        } catch (SQLException e) {
            System.out.println("更新操作中出现错误！！！");
            System.out.println(e);
        } finally {
            DBConnection.close(conn, pstmt,null);
        }
        return updateSuccessFlag;
    }
    
    
    public boolean MofifyPassword(String id,String password) {
        String sql = "UPDATE user_table SET user_pass=? WHERE user_id=?";
        Connection conn = DBConnection.getConnection();
        PreparedStatement pstmt = null;
        boolean updateSuccessFlag = false;
        try {
            pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, password);
            pstmt.setString(2, id);
            int x = pstmt.executeUpdate();
            if (x > 0) {
                updateSuccessFlag = true;
            }
        } catch (SQLException e) {
            System.out.println("更新操作中出现错误！！！");
            System.out.println(e);
        } finally {
            DBConnection.close(conn, pstmt,null);
        }
        return updateSuccessFlag;
    }
    // 删除操作

    public boolean delete(int id) {
        String sql = "DELETE FROM user_table WHERE user_id=?";
        PreparedStatement pstmt = null;
        Connection conn = DBConnection.getConnection();
        boolean deleteSuccessFlag = false;
        try {
            pstmt = conn.prepareStatement(sql);
            pstmt.setInt(1, id);
           int i= pstmt.executeUpdate();
           if(i>0){
            deleteSuccessFlag = true;
           }
        } catch (SQLException e) {
            System.out.println("删除操作中出现错误！！！");
            System.out.println(e);
        } finally {
            DBConnection.close(conn, pstmt, null);
        }
        return deleteSuccessFlag;
    }
    // 按ID查询

    public User queryById(int id) {
        User user = null;
        String sql = "SELECT * FROM user_table WHERE user_id=?";
        Connection conn = DBConnection.getConnection();
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        try {
            pstmt = conn.prepareStatement(sql);
            pstmt.setInt(1, id);
            rs = pstmt.executeQuery();
            //此处为什么不用while,用while可不可以
            if (rs.next()) {
                user = new User();
                user.setId(rs.getInt(1));
                user.setName(rs.getString(2));
                user.setPass(rs.getString(3));
                user.setEmail(rs.getString(4));
            }

        } catch (SQLException e) {
            System.out.println("操作中出现错误！！！");
        } finally {
            DBConnection.close(conn, pstmt, rs);
        }
        return user;
    }

    public int findByIdAndPassword(String id, String password) {
        Connection conn = DBConnection.getConnection();
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        int result=0;
        try {
            String querySql = "select user_pass from user_table "
                    + " where user_id=? ";
            pstmt = conn.prepareStatement(querySql);
            pstmt.setString(1, id);
            rs = pstmt.executeQuery();
            if (rs.next()) {
                if (password.equals(rs.getString("user_pass"))) {
                    result=1;
                } else {
                    result=2;
                }
            }

        } catch (SQLException e) {
            System.out.println("数据库操作错误");
            System.out.println(e.getMessage());
        } finally {
             DBConnection.close(conn, pstmt, rs);
        }
        return result;
    }
}
